package com.fieldchain.ex.fa100.ui.access;

import android.content.Intent;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.access.Constant;
import com.fieldchain.ex.fa100.base.MyBaseFragment;
import com.fieldchain.ex.fa100.bean.C2CConfig;
import com.fieldchain.ex.fa100.ui.c2c.activity.C2CHistoryActivity;
import com.fieldchain.ex.fa100.ui.c2c.activity.GuizeActivity3;
import com.fieldchain.ex.fa100.ui.c2c.fragment.C2CBuyFragment;
import com.fieldchain.ex.fa100.ui.c2c.fragment.C2CSellFragment;
import com.fieldchain.ex.fa100.ui.personal.activity.PersonalCanterActivity;

import com.fieldchain.ex.fa100.utils.net.access.HttpRequest;
import com.fieldchain.ex.fa100.utils.net.modle.BeanRequest;
import com.fieldchain.ex.fa100.utils.net.modle.StringRequest;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/16
 */
public class C2CFragment extends MyBaseFragment {
    private ImageView ivPersonal;
    private TextView tvTitle;
    private RadioGroup rg;
    private RadioButton rbBuy;
    private RadioButton rbSell;
    private TextView tvHistory;
    private LinearLayout linRule;
    private ImageView ivGuize;
    private FrameLayout frm;
    private C2CBuyFragment c2CBuyFragment;
    private C2CSellFragment c2CSellFragment;
    private int index;

    private String market;
    private boolean needRemote;
    private int type = 0;

    @Override
    protected void findView() {
        ivPersonal = view.findViewById(R.id.iv_personal);
        tvTitle = view.findViewById(R.id.tv_title);
        rg = view.findViewById(R.id.rg);
        rbBuy = view.findViewById(R.id.rb_buy);
        rbSell = view.findViewById(R.id.rb_sell);
        tvHistory = view.findViewById(R.id.tv_history);
        linRule = view.findViewById(R.id.lin_rule);
        ivGuize = view.findViewById(R.id.iv_guize);
        frm = view.findViewById(R.id.frm);
    }

    @Override
    protected void setValue() {
        needProgress=false;
        initFragemnt();
        initClick();
        initSwitch();
        getCoin();
        getData();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (needRemote) {
            if (type == 0) {
                rbBuy.setChecked(true);
            } else if (type == 1) {
                rbSell.setChecked(true);
            } else if (type == 2) {
                startActivity(new Intent(mContext, C2CHistoryActivity.class));
            }
        }

        isFirst = false;
    }

    @Override
    public int getLayout() {
        return R.layout.fragment_c2c;
    }

    private void initSwitch() {
        rbBuy.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    index = 0;
                    ivGuize.setImageDrawable(getResources().getDrawable(R.drawable.icon_wenred));
                    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
                    if (!c2CBuyFragment.isAdded()) {
                        transaction.hide(c2CSellFragment).add(R.id.frm, c2CBuyFragment).commit();
                    } else {
                        transaction.hide(c2CSellFragment).show(c2CBuyFragment).commit();
                    }
                }
            }
        });
        rbSell.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    index = 1;
                    ivGuize.setImageDrawable(getResources().getDrawable(R.drawable.icon_wengreen));
                    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
                    if (!c2CSellFragment.isAdded()) {
                        transaction.hide(c2CBuyFragment).add(R.id.frm, c2CSellFragment).commit();
                    } else {
                        transaction.hide(c2CBuyFragment).show(c2CSellFragment).commit();
                    }
                }
            }
        });

    }


    public void initFragemnt() {
        c2CBuyFragment = new C2CBuyFragment();
        c2CSellFragment = new C2CSellFragment();
        FragmentTransaction transaction_home = getChildFragmentManager().beginTransaction();
        transaction_home.add(R.id.frm, c2CBuyFragment);
        transaction_home.commit();
    }

    public void initClick() {
        ivPersonal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(mContext, PersonalCanterActivity.class));
            }
        });
        tvHistory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(mContext, C2CHistoryActivity.class));
            }
        });
        linRule.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(mContext, GuizeActivity3.class));
            }
        });
    }

    public void refresh(String market, int type) {
        needRemote = true;
        this.type = type;
    }

    @Override
    public void onPause() {
        super.onPause();
        needRemote = false;
    }

    public void getData() {
        HttpRequest.getInstance().getRequest(Constant.c2cCoin, new StringRequest() {
            @Override
            public void onSucess(String str, int code, String msg) {

            }

            @Override
            public void onFaild(Exception e) {

            }
        });
    }

    @Override
    public void loginShow() {

    }

    public void getCoin() {
        HttpRequest.getInstance().getRequest(Constant.c2cconfig, new BeanRequest<C2CConfig>() {
            @Override
            public void onSucess(C2CConfig bean, int code, String msg) {
                if (code == 200) {
                    tvTitle.setText(bean.getCoinName().toUpperCase());
                    refreshChild(bean.getCoinName().toUpperCase());
                }
            }

            @Override
            public void onFaild(Exception e) {

            }
        });
    }

    public void refreshChild(String coin){
        c2CBuyFragment.refresh(coin);
        c2CSellFragment.refresh(coin);
    }
}
